package com.example.validated.controller;

import com.example.validated.annotation.ValidGroups;
import com.example.validated.annotation.VerifySeq;
import com.example.validated.vo.UserValidated;
import com.example.validated.vo.VerifySeqDTO;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Objects;

@Validated
@RestController
@RequestMapping("/onMethod")
public class OnMethodController {
    @GetMapping("/user")
    public String user(@NotBlank(message = "msgId不能为空") String msgId, @NotNull(message = "是否推荐不能为null") Integer type) {
        return "ok";
    }

    @GetMapping("/verifySeq")
    public String user(@Validated(VerifySeq.class) VerifySeqDTO verifySeqDTO) {
        return "ok";
    }

    // 验证可进行分组。
    @PostMapping("/updateUser")
    public String updateUser(@Validated(value = {ValidGroups.Update.class,ValidGroups.Default.class}) @ModelAttribute @RequestBody UserValidated user, BindingResult bindResult) {
        // 如果有错误
        if (bindResult.hasErrors()) {
            // 返回提示信息
            return Objects.requireNonNull(bindResult.getFieldError()).getDefaultMessage();

        }
        UserValidated user1 = new UserValidated();
        return "ok";
    }
}
